package com.wjs.gulimall.seckill.controller;

import com.atguigu.common.utils.R;
import com.wjs.gulimall.seckill.service.SecKillService;
import com.wjs.gulimall.seckill.to.SecKillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class SecKillController {
    @Autowired
    private SecKillService secKillService;

    @GetMapping("/currentSeckillSkus")
    @ResponseBody
    public R getCurrentSeckillSkus(){
        List<SecKillSkuRedisTo> list=secKillService.getCurrentSeckillSkus();
        return R.ok().setData(list);
    }

    @GetMapping("/sku/seckill/{skuId}")
    @ResponseBody
    public R getSkuSecKillInfo(@PathVariable("skuId")Long skuId){
        SecKillSkuRedisTo redisTo=secKillService.getSkuSecKillInfo(skuId);
        return R.ok().setData(redisTo);
    }

    @GetMapping("/kill")
    public String secKill(@RequestParam("killId")String killId,
                     @RequestParam("key")String key,
                     @RequestParam("num")Integer num,
                     Model model){
        String OrderSn=secKillService.kill(killId,key,num);
        model.addAttribute("orderSn",OrderSn);
        return "success";

    }
}
